package com.heima.freemarker;

import com.heima.freemarker.pojo.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
@RunWith(SpringRunner.class)
public class GenerHtml {

    @Autowired
    private Configuration configuration;
    /**
     * 利用模板+数据模型可以生成静态html文件
     */
    @Test
    public void gengerHtml(){
        //1.获取模板
        try {
            Template template = configuration.getTemplate("02-list.ftl");
            //2.获取数据模型
            Map resultMap = dataModel();
            //3.生成静态html文件
            template.process(resultMap,new FileWriter("D:\\list.html"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Map dataModel(){
        //list遍历
        Student student=new Student();
        student.setName("张三");
        student.setAge(123);
        student.setMoney(100);

        Student student2=new Student();
        student2.setName("李四");
        student2.setAge(12);
        student2.setMoney(80);

        List<Student> list=new ArrayList();
        list.add(student);
        list.add(student2);

        Map resultMap=new HashMap();

        resultMap.put("stus",list);

        //map遍历
        Map map=new HashMap<>();
        map.put("stu1",student);
        map.put("stu2",student2);
        resultMap.put("stuMap",map);

        return resultMap;
    }
}
